/*
 *  Copyright 2009 Matthieu Chase Heimer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.macro10.xroads.commands;

import com.macro10.switchboard.AbstractCommand;
import com.macro10.switchboard.Request;
import com.macro10.switchboard.Session;
import com.macro10.switchboard.entity.DataObject;
import com.macro10.switchboard.service.UserTransaction;
import com.macro10.xroads.entity.Avatar;
import com.macro10.xroads.service.AvatarService;
import java.util.Collection;

/**
 * Requires headers: avatar
 * Filters: require_login
 */
public class GetAvatar extends AbstractCommand {

    @Override
    public ExecuteResult execute(Request request, Session session) {
        String username = (String) session.getAttribute("username");
        String avatarId = request.getHeader("avatar");

        UserTransaction ut = null;
        try {
            ut = new UserTransaction();
            AvatarService avatarService = new AvatarService(ut);
            Avatar avatar = avatarService.getAvatar(avatarId);
            if (avatar != null) {
                if (avatar.getAccount() == null || !avatar.getAccount().getClientId().equals(username)) {
                    session.writeError(request.getURL() + ":avatar.not.yours");
                } else {
                    Collection<DataObject> dataObjects = avatar.getObjects();
                    session.writeText(request.getURL() + ":avatar.details");
                    session.writeText("avatar:" + avatar);
                    session.writeText("account:" + avatar.getAccount());
                    session.writeText("online:" + avatar.isOnline());
                    if (avatar.getArea() != null) {
                        session.writeText("area:" + avatar.getArea().getAreaName());
                    }
                    for (DataObject obj : dataObjects) {
                        session.writeText(obj.getName() + ":" + obj.getValue());
                    }
                }
            } else {
                session.writeError(request.getURL() + ":avatar.not.found");
            }
            ut.commit();
        } catch (Exception e) {
            session.writeException(request.getURL() + ":avatar.retrieval.failed", e);
            if (ut != null) {
                ut.rollback();
            }
        }
        return ExecuteResult.START_POST;
    }
}
